<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.smarthome.web.property.utils.UserHolder"  %>
<!DOCTYPE html>
<html>
	<head>
		<title>短信模版</title>
		<jsp:include page="../../views/layout/inc.jsp"></jsp:include>
		<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/userController/editPage')}"> --%>
			<script type="text/javascript">
				$.canAdd = true;
			</script>
		<%-- </c:if> --%>
		<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/userController/delete')}"> --%>
			<script type="text/javascript">
				$.canEdit = true;
			</script>
		<%-- </c:if> --%>
			<script type="text/javascript">
				$.canDelete = true;
			</script>
		<script type="text/javascript">
			var dataGrid;
			$(function() {
				dataGrid = $('#dataGrid').datagrid({
					url : '/messageTemplateController/dataGrid',
					fit : true,
					fitColumns : true,
					border : false,
					pagination : true,
					idField : 'id',
					pageSize : 10,
					pageList : [ 10, 20, 30, 40, 50 ],
					sortName : 'gmtCreate',
					sortOrder : 'desc',
					checkOnSelect : false,
					selectOnCheck : false,
					singleSelect : true,
					nowrap : false,
					frozenColumns : [ [ {
						field : 'id',
						title : '编号',
						width : 150,
						hidden : true,
						checkbox : true
					}, {field : 'residenceName',
						title : '小区名字',
						width : 100
					}, {
						field : 'typeName',
						title : '短信类型',
						width : 120
					} ] ],
					columns : [ [ {
						field : 'content',
						title : '模版内容',
						width : 200
					}, {
						field : 'isUsed',
						title : '启用状态',
						width : 80,
						formatter:function(val,rec){ 
							if(val == 0) {
								return '停用';
							}else if(val == 1) {
								return '启用';
							}
						}
					}, {
						field : 'gmtCreate',
						title : '创建时间',
						width : 150,
						sortable : true,
						formatter:function(val,rec){ 
							 var unixTimestamp = new Date(val);  
		                     return unixTimestamp.format("yyyy-MM-dd hh:mm:ss");  
						}
					}, {
						field : 'gmtModify',
						title : '更新时间',
						width : 150,
						sortable : true,
						formatter:function(val,rec){ 
							 var unixTimestamp = new Date(val);  
		                     return unixTimestamp.format("yyyy-MM-dd hh:mm:ss");  
						}
					}, {
						field : 'action',
						title : '操作',
						width : 100,
						formatter : function(value, row, index) {
							var str = '';
							if ($.canEdit) {
								str += $.formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="修改短信模版"/>', row.id, '/images/extjs_icons/pencil.png');
							}
							str += '&nbsp;';
							if ($.canDelete) {
								str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除短信模版"/>', row.id, '/images/extjs_icons/cancel.png');
							}
							return str;
						}
					} ] ],
					toolbar : '#toolbar',
					onLoadSuccess : function() {
						parent.$.messager.progress('close');
						$(this).datagrid('tooltip');
					},
					onRowContextMenu : function(e, rowIndex, rowData) {
						e.preventDefault();
						$(this).datagrid('unselectAll').datagrid('uncheckAll');
						$(this).datagrid('selectRow', rowIndex);
					}
				});
			});
			
			function editFun(id) {
				parent.$.modalDialog({
					title : '修改短信模版',
					width : 600,
					height : 300,	
					href : '/messageTemplateController/editPage?id=' + id,
					buttons : [ {
						text : '确认修改',
						handler : function() {
							parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
							var f = parent.$.modalDialog.handler.find('#form');
							f.submit();
						}
					} ]
				});
			}
			
			function deleteFun(id) {
				parent.$.messager.confirm('询问', '您是否要删除当前模版？', function(b) {
					if (b) {
						parent.$.messager.progress({
							title : '提示',
							text : '数据处理中，请稍后....'
						});
						$.post('/messageTemplateController/delete', {
							id : id
						}, function(result) {
							if (result.success) {
								dataGrid.datagrid('reload');
							}
							parent.$.messager.alert('提示', result.msg, 'info');
							parent.$.messager.progress('close');
						}, 'JSON');
					}
				});
			}
			
			function addFun() {
				parent.$.modalDialog({
					title : '新增模版',
					width : 600,
					height : 300,
					href : '/messageTemplateController/addPage',
					buttons : [ {
						text : '确认新增',
						handler : function() {
							parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
							var f = parent.$.modalDialog.handler.find('#form');
							f.submit();
						}
					} ]
				});
			}
			
			function searchFun() {
				dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
			}
			function cleanFun() {
				$('#searchForm input').val('');
				dataGrid.datagrid('load', {});
			}
		</script>
	</head>
	<body>
		<div class="easyui-layout" data-options="fit : true,border : false">
				<%if(UserHolder.isSuperResidence()){ %>
				<div data-options="region:'north',title:'查询条件',border:false" style="height: 60px; overflow: hidden;">
					<form id="searchForm">
						<table class="table table-hover table-condensed">
						<tr>
							<th>小区名字</th>
							<td><input name="residenceId" class="easyui-combobox"   data-options=" url: '/residenceController/combobox',icons:[{iconCls:'icon-clear',handler: function(e){$(e.data.target).combobox('setValue','');}}]"/></td>
						</tr>
					</table>
					</form>
					</div>
				<%} %>
			<div data-options="region:'center',border:false">
				<table id="dataGrid"></table>
			</div>
		</div>
		<div id="toolbar" style="display: none;">
			<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
			<%if(UserHolder.isSuperResidence()){ %>
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();">提交查询</a>
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="cleanFun();">重置查询</a>
			<%} %>
		
		</div>
	</body>
</html>